package edu.upb.acs.jobsmaps.activity.adapter;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;
import edu.upb.acs.jobsmaps.AddressLookup;
import edu.upb.acs.jobsmaps.R;
import edu.upb.acs.jobsmaps.database.task.GetCompanyAfterIdTask;
import edu.upb.acs.jobsmaps.entity.Company;
import edu.upb.acs.jobsmaps.entity.Job;

import java.util.Map;

/**
 * Created by dani on 5/15/2014.
 */
public class MapMarkerAdapter extends Activity implements GoogleMap.InfoWindowAdapter {

	private static final String TAG = MapMarkerAdapter.class.getName();
	private final Map<Integer, Job> jobs;
    private final ContentResolver contentResolver;
    private final Context applicationContext;
	private final LayoutInflater layoutInflater;
	private final Map<Integer, Company> companies;

	public MapMarkerAdapter(LayoutInflater layoutInflater, Map<Integer, Job> jobs, Map<Integer, Company> companies, ContentResolver contentResolver, Context applicationContext) {
        this.layoutInflater = layoutInflater;
        this.jobs = jobs;
		this.companies = companies;
        this.contentResolver = contentResolver;
        this.applicationContext = applicationContext;
    }

    // Use default InfoWindow frame
    public View getInfoWindow(Marker arg0) {
        return null;
    }

    // Defines the contents of the InfoWindow
    @Override
    public View getInfoContents(Marker marker) {
		String identifier = marker.getSnippet();
		Log.d(TAG, "Upload marker info with identifier = " + identifier);
		if (identifier.startsWith("J")){
			View jobInfoVIew = layoutInflater.inflate(R.layout.marker_job_info, null);
			String jobId = identifier.substring(1);
			Log.d(TAG, "Search after job with id = " + jobId);
			final Job job = jobs.get(Integer.valueOf(jobId));
			if (job == null){
				Log.d(TAG, "No job found. Return....");
				return jobInfoVIew;
			} else {
				Log.d(TAG, "Found job.. Drawing");
			}

			TextView jobTitle = (TextView) jobInfoVIew.findViewById(R.id.jobMarkerJobTitle);
			jobTitle.setText(job.getName());
			jobTitle.setPaintFlags(jobTitle.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

			AddressLookup addressLookup = new AddressLookup(job.getLatitude(), job.getLongitude(), applicationContext );
			TextView jobAddress = (TextView) jobInfoVIew.findViewById(R.id.jobMarkerJobAddress);
			jobAddress.setText(addressLookup.getAddress());

			//crop if the text is longer than 30 characters
			if (null != job.getDescription()) {
				TextView jobDescription = (TextView) jobInfoVIew.findViewById(R.id.jobMarkerJobDescription);
				StringBuilder description = new StringBuilder(job.getDescription());
				if (description.length() > 30) {
					description.replace(30, description.length(), "...");
				}
				jobDescription.setText(description.toString());
			}

			TextView jobPostedDate = (TextView) jobInfoVIew.findViewById(R.id.jobMarkerPostedDate);
			jobPostedDate.setText(job.getPostedDate().toString());

			Company company = null;
			GetCompanyAfterIdTask getCompanyAfterIdTask = new GetCompanyAfterIdTask();
			try {
				company = getCompanyAfterIdTask.execute(job.getCompanyId()).get();
				Log.d(TAG, "Got company = " + company);
			} catch (Exception e) {
				Log.e(TAG, "Can not get company", e);
			}

			if (company == null){
				Log.e(TAG, "No company found. Return....");
				//TODO display error message in the UI
			} else {
				TextView companyName = (TextView) jobInfoVIew.findViewById(R.id.jobMarkerCompanyName);
				companyName.setText(company.getName());

				TextView companyProfile = (TextView) jobInfoVIew.findViewById(R.id.jobMarkerCompanyProfile);
				companyProfile.setText(company.getProfile());
			}

			return jobInfoVIew;
		} else if (identifier.startsWith("C")){
			String companyId = identifier.substring(1);

			Log.d(TAG, "Search after company with id = " + companyId);

			Company company = companies.get(Integer.valueOf(companyId));
			if (company == null){
				Log.d(TAG, "No company found. Return....");
				return null;
			} else {
				Log.d(TAG, "Found company.. Drawing");
			}

			View companyInfoVIew = layoutInflater.inflate(R.layout.marker_company_info, null);

			TextView companyName = (TextView) companyInfoVIew.findViewById(R.id.companyMarkerCompanyName);
			companyName.setText(company.getName());

			AddressLookup addressLookup = new AddressLookup(company.getLatitude(), company.getLongitude(), applicationContext );
			TextView jobAddress = (TextView) companyInfoVIew.findViewById(R.id.companyMarkerCompanyAddress);
			jobAddress.setText(addressLookup.getAddress());

			TextView companyProfile = (TextView) companyInfoVIew.findViewById(R.id.companyMarkerCompanyProfile);
			companyProfile.setText(company.getProfile());

			if (null != company.getDescription()) {
				TextView jobDescription = (TextView) companyInfoVIew.findViewById(R.id.companyMarkerCompanyDescription);
				StringBuilder description = new StringBuilder(company.getDescription());
				if (description.length() > 30) {
					description.replace(30, description.length(), "...");
				}
				jobDescription.setText(description.toString());
			}

			return companyInfoVIew;
		} else {
			Log.d(MapMarkerAdapter.class.getName(), "Unsupoorted identifer to draw a marker");
			return null;
		}
    }

}
